{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Termites\n", "\n", "Consider a simple termite that only follows these simple rules:\n", "\n", "1. If you see a wood chip, and you are not carrying one, pick it up\n", "2. If you see a wood chip, and you are carrying one, then drop it\n", "3. Otherwise, move in your default direction \n", "4. Every once in awhile, change direction\n", "\n", "Now, imagine a few of these termites roaming around a flat world, scattered with wood chips. What would happen? How can we model this world?\n", "\n", "## Simulation\n", "\n", "First, we must identify the objects we wish to model. Possibilities include: \n", "\n", "* termites\n", "* world\n", "* wood chips\n", "\n", "First, let's consider what properties a termite should have:\n", "\n", "* position in the world\n", "* whether it is carrying a wood chip or not\n", "* direction heading (vector, velocity on x and y)\n", "\n", "Also, what actions must a termite be able to do:\n", "\n", "* move in the world\n", "* pick up a wood chip\n", "* drop a wood chip\n", "* draw itself\n", "\n", "We might want to add other methods, but it needs to at least support those.\n", "\n", "## Termite\n", "\n", "So, let's put together a termite class. It might look something like:\n", "\n", "```java\n", "class Termite {\n", " int x;\n", " int y;\n", " int vx;\n", " int vy;\n", " int carrying;\n", " \n", " Termite(int x, int y) { //constructor\n", " this.x = int(x);\n", " this.y = int(y);\n", " this.randomizeDirection();\n", " this.carrying = 0;\n", " }\n", "\n", " ...\n", "}\n", "```\n", "\n", "In thinking about the world, and wood chips, it might be that representing them as an object might be overkill. For example, if we imagine the world as a 2D matrix, then we could represent the presence of food with a 1, and the absence of food with a 0. Likewise, carrying can just be a 1 (for yes), and 0 (for no).\n", "\n", "It may be that instead of having termites bounce off of the walls (like a ball simulation) that we instead have them \"wrap\" around (like a [torus](https://en.wikipedia.org/wiki/Torus)). For example, if a termite goes too far to the left, it will wrap around to the right, and instantly appear there. Likewise, if a turtle goes below 0 or above height, then it wraps back around. Thus, we implement an infinite, repeating surface.\n", "\n", "This putting it all together might look like the following. \n", "\n", "Note:\n", "\n", "* termites are represented by ovals. You could make them face the correct direction, or draw legs on them.\n", "* termites turn red when they are carrying a wood chip\n", "* wood chips are represented by red squares" ] }, { "cell_type": "code", "execution_count": 86, "metadata": { "collapsed": false }, "outputs": [ { "data": { "text/html": [ "\n", "